package beans;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookException.BookException;
import bookException.BookExceptionCode;


public abstract class GeneralBean {
	private HttpServletRequest request = null;
	private HttpServletResponse response = null;
	private boolean errorFlag = false;
	private BookException exception = null;
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public HttpServletResponse getResponse() {
		return response;
	}
	
	public void setResponse(HttpServletResponse response) throws UnsupportedEncodingException {
		this.response = response;
		request.setCharacterEncoding("UTF-8");
	}
	
	public boolean isError() {
		return errorFlag;
	}
	
	public void setBookException(BookException exception) {
		this.exception = exception;
		this.errorFlag = true;
	}
	
	public BookException getBookException() {
		return exception;
	}
		
	public String getStringFromParam(String name){
		return getRequest().getParameter(name) == null?"":getRequest().getParameter(name);
	}
	
	public int getIntFromParam(String name){
		try{
			return Integer.parseInt(getRequest().getParameter(name));
		} catch (Exception e) {
			return 0;
		}
	}
	
	public boolean getBoolFromParam(String name){
		try{
			return getRequest().getParameterValues(name) == null?false:true;
		} catch (Exception e) { 
			return errorFlag;	
		}		
	}
	
	public boolean isParamExist(String name){
		if(getRequest() == null)
			return false;
		
		return getRequest().getParameter(name) == null?false:true;
	}

	public BookException getException() {
		return exception;
	}

	public void setException(BookException exception) {
		this.exception = exception;
	}
}
